<?php
namespace app\teacher\controller;

use app\teacher\controller\PublicController;
use app\common\model\TemplateGroupModel;
use app\common\model\RelationClassModel;
use app\common\model\RelationTeacherModel;
use app\common\model\RelationStudentModel;
use app\common\model\RecordModel;
use app\common\model\RecordClassModel;
use app\common\model\AppUserModel;
use app\common\logic\home\Template;

//模板发放
class RecordController extends PublicController
{

    public function __construct(){
        parent::__construct();
        $this->check();
        $this->ids = $this->install();
    }

    public function index(){
        $param = $this->request->param();
        $data_app_user = AppUserModel::where('id','in',$this->ids)->field('realName,id')->select();

        $data = TemplateGroupModel::where(function($query) use($param){
                                    if(!empty($param['create_time'])){
                                        $time = strtotime($param['create_time']);
                                        $query->where('create_time','>=',date('Y-m-d H:i:s',$time))->where('create_time','<',date('Y-m-d H:i:s',$time+60*60*24));
                                    }
                                    if(empty($param['t_user_id'])){
                                        $query->where('user_id','in',$this->ids);
                                    }else{
                                        $query->where('user_id',$param['t_user_id']);
                                    }
                                    if(!empty($param['group_id'])){
                                        $query->where('group_id',$param['group_id']);
                                    }
                                })
                                ->paginate(20,false,['query'=>$param]);
        $this->assign('data',$data);
        $this->assign('data_app_user',$data_app_user);
        return $this->fetch();
    }
    
    public function add(){
        $param = $this->request->param();
        $data_group = TemplateGroupModel::where('group_id','in',$param['vals'])->select();
        $data = RelationTeacherModel::hasWhere('classs',function($query) use($param){
                                    if(!empty($param['create_time'])){
                                        $time = strtotime($param['create_time']);
                                        $query->where('create_time','>=',date('Y-m-d H:i:s',$time))->where('create_time','<',date('Y-m-d H:i:s',$time+60*60*24));
                                    }
                                })
                                ->where('t_relation_teacher.user_id',$this->user_id)
                                ->where(function($query) use($param){

                                    if(!empty($param['class_id'])){
                                        $query->where('t_relation_teacher.class_id',$param['class_id']);
                                    }
                                })
                                ->paginate(20,false,['query'=>$param])
                                ->each(function($value,$item){
                                   // dump($value['class_id']);
                                    $value['count'] = RelationStudentModel::where('class_id',$value['class_id'])->count('student_id');
                                });

        $this->assign('param',$param);
        $this->assign('page',$data->render());
        $this->assign('data_group',$data_group);
        $this->assign('data',$data);
        return $this->fetch();
    }
    //发送模板
    public function toAdd(Template $Template){
        $param = $this->request->param();
        if(empty($param['vals'])){
            $this->error('请必须选择模板');die;
        }
        if(empty($param['ids'])){
            $this->error('请必须选择班组');die;
        }
        foreach ($param['ids'] as $key => $value) {
            $data = RelationStudentModel::where('class_id',$value)->select();
            $status = RelationClassModel::where('class_id',$value)->value('status');
            foreach ($param['vals'] as $ke => $valu) {
                $data_record_class = [
                    't_user_id' => $this->user_id,
                    'class_id'  => $value,
                    'group_id' => $valu,
                    'status' => $status,
                    'create_time' => date('Y-m-d H:i:s')
                ];
                $record_class_id = RecordClassModel::insertGetId($data_record_class);

                foreach ($data as $k => $val) {
                    $mytask_id = $Template->index1($valu,$val['user_id']);
                    $data_record = [
                        's_user_id' => $val['user_id'],
                        't_user_id' => $this->user_id,
                        'class_id'  => $value,
                        'status'  => $val['status'],
                        'group_id' => $valu,
                        'mytask_id' => $mytask_id,
                        'record_class_id' => $record_class_id,
                        'create_time' => date('Y-m-d H:i:s')
                    ];
                    $rs = RecordModel::insert($data_record);
                }
            }
               
            

        }

        $this->success('发送成功','index');die;
    }
    //课题发放记录
    public function list1(){
        $param = $this->request->param();
        $data = TemplateGroupModel::where('user_id','in',$this->ids)->paginate(20,false,['query'=>$param]);
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function list1Detail(){
        $param = $this->request->param();
        $data = RecordClassModel::where('group_id',$param['group_id'])
                        ->where(function($query) use($param){
                            if(!empty($param['create_time'])){
                                $time = strtotime($param['create_time']);
                                $query->where('create_time','>=',date('Y-m-d H:i:s',$time))->where('create_time','<',date('Y-m-d H:i:s',$time+60*60*24));
                            }
                        })
                        ->order('record_class_id desc')
                        ->paginate(20,false,['query'=>$param])
                        ->each(function($value,$item){
                               // dump($value['class_id']);
                                $value['count'] = RelationStudentModel::where('class_id',$value['class_id'])->count('student_id');
                            });

         $this->assign('data',$data);
         $this->assign('page',$data->render());
        return $this->fetch();
    }


    public function list2(){
        $param = $this->request->param();
        $data = RelationClassModel::where('class_id','in',function($query){
             $query->table('t_relation_teacher')->where('user_id','in',$this->ids)->distinct(true)->field('class_id');
        })->paginate(20)->each(function($value,$item){
           // dump($value['class_id']);
            $value['count'] = RelationStudentModel::where('class_id',$value['class_id'])->count('student_id');
        });

        $this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch();
    }


    public function list2Detail(){
        $param = $this->request->param();
         $data = RecordClassModel::where('class_id',$param['class_id']) 
                        ->where(function($query) use($param){
                            if(!empty($param['create_time'])){
                                $time = strtotime($param['create_time']);
                                $query->where('create_time','>=',date('Y-m-d H:i:s',$time))->where('create_time','<',date('Y-m-d H:i:s',$time+60*60*24));
                            }
                        })->order('record_class_id desc')->paginate(20,false,['query'=>$param]);

        $this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch();
    }

}
